MyDesktop.StorageWindow = Ext.extend(Ext.app.Module, {
    id:'storage-win',
    init : function(){
        this.launcher = {
            text: '仓库管理',
            iconCls:'icon-grid',
            handler : this.createWindow,
            scope: this
        }
    },
	
	cm_storage : new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
	{header : '仓库名称',dataIndex : 'name'},
	{header : '简拼',dataIndex : 'pinyin'},
	{header : '仓库地址',dataIndex : 'address'}
	]),
	
	cm_storageSpace : new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), 
	{header : '仓位',width : 120,dataIndex : 'name'}
	]),

	getPanel:function(){
			
				var record;		
		 		var store=new Ext.data.Store({
                     url : app.url+'/admin/com/storageList.do',
					 baseParams:{start:0,limit:20},
					 autoLoad : true,
	                 reader : new Ext.data.JsonReader({totalProperty : 'totalProperty',root : 'root'}, 
	                 [{name : 'id',mapping : 'id',type : 'int'}, 
	                  {name : 'name',mapping : 'name',type : 'string'},
					  {name : 'pinyin',mapping : 'pinyin',type : 'string'},
	                  {name : 'address',mapping : 'address',type : 'string'}
					 ])
                });
			
				var ds_storageSpace_data = new Ext.data.Store({
	               url : app.url+'/admin/com/getStorageSpaces.do',
	               reader : new Ext.data.JsonReader({root : 'root'}, [{
				   name : 'id'},
				   {name : 'name'}
				   ])
                });	
                
		 	   var grid_storage=new Ext.grid.GridPanel({
                        border:false,
						height:400,
	                    region : 'center',
                        ds: store,
                        cm: this.cm_storage,

                        viewConfig: {
                            forceFit:true
                        },
                        tbar:[{
                            text:'增加仓库',
                            tooltip:'增加仓库',
                            iconCls:'add',
                            handler:addStorage
                        }, '-', {
                            text:'修改仓库',
                            tooltip:'修改仓库',
                            iconCls:'option',
                            handler:editStorage
                        },'-',{
                            text:'删除仓库',
                            tooltip:'删除仓库',
                            iconCls:'remove'
                        }],
						bbar: new Ext.PagingToolbar({
                              pageSize: 20,
                              store: store,
                              displayInfo: true,
							  plugins: new Ext.ux.ProgressBarPager()
                         }),
                         listeners : {
		                   'click' : function(){
			                  record = this.getSelectionModel().getSelected();
			                  ds_storageSpace_data.baseParams.id = record.data.id; 
			                  ds_storageSpace_data.load();
		                 }}                      
                    });	
					
			var grid_storageSpace_detail = new Ext.grid.EditorGridPanel({
	                  title : '仓位信息',
	                  iconCls : 'icon-grid',
	                  region: 'center',
	                  loadMask : {msg : '数据加载中...'},
	                  width:400,
                      cm : this.cm_storageSpace,
	                  ds : ds_storageSpace_data,
	                  sm : new Ext.grid.RowSelectionModel({singleSelect : true}),
	                  enableColumnMove : false,
	                  trackMouseOver : false,
	                  frame : true,
	                  clicksToEdit : 1
            });
            
            var storageSpace_tabs=new Ext.TabPanel({
                activeTab:0,
                region: 'south',
                height:200,
                frame:true,
                items:[grid_storageSpace_detail]
           });
           
         
           
            var storage_detailForm = new Ext.FormPanel({
       		 	 labelSeparator : "：",
				 frame:true,
				 border:false,
				 defaultType : 'textfield',
				 defaults:{width:160},
				 items:[{
			              xtype:'hidden',
			              name:'id'
			            },{				     	
				      	fieldLabel:'仓库名称',
				      	name:"name",
						allowBlank : false,
			            minLength : 3,
			            maxLength : 30
				     },{				     	
				     	fieldLabel:'仓库地址',
				      	name:"address",
						allowBlank : false,
			            minLength : 6,
			            maxLength : 30
				     }],
				 buttons:[
			       {
			     	id:'commitButton',
					iconCls : 'save',
			        handler:submitForm
			       },{
			     	 id:'closeButton',
					 text : '关闭',
					 iconCls : 'close',
			     	 handler:function(){				     		
			     	    addOrEditStorage_win.hide();
			     	}
			      }
			     ]
       		 });
       		 
       		 var addOrEditStorage_win = new Ext.Window({
       		 	layout:'fit',
		    	width:330,
		    	closeAction:'hide',
		   		height:200,
				resizable :false,
				shadow : true,
				modal :true,
		   	    closable:true,
		    	bodyStyle:'padding:5 5 5 5',		    	
		    	animCollapse:true,
				items:[storage_detailForm]
       		 });
       		 
       		 var storage_gridPanel =  new Ext.grid.GridPanel({					   
                        border:false,
						columnLines :true,
                        enableHdMenu : false,
						loadMask : {msg : '数据加载中...'},
                        ds: store,
                        cm: this.cm_storage,						
                        viewConfig: {
                            forceFit:true
                        },
                        tbar:[{
                            text:'增加',
                            tooltip:'增加仓库信息',
                            iconCls:'add',
                            handler:addStorage
                        }, '-', {
                            text:'修改',
                            tooltip:'修改仓库信息',
                            iconCls:'option',
                            handler:editStorage
                        },'-',{
                            text:'删除',
                            tooltip:'删除仓库信息',
                            iconCls:'delete'
                        }],
						bbar: new Ext.PagingToolbar({
                            pageSize: 20,
                            store: store,
                            displayInfo: true,
                            plugins: new Ext.ux.ProgressBarPager()
                        })
            });
                                
            
            function addStorage(){
            	if(storage_detailForm.form.el){
            		storage_detailForm.form.el.dom.reset();            	
            	}else{
            		storage_detailForm.form.reset();
            	}
                Ext.getCmp('commitButton').setText('新增');
		        addOrEditStorage_win.isAdd = true;
		        addOrEditStorage_win.setTitle("新增仓库");
		        addOrEditStorage_win.show();
            }
            
            function editStorage(){
            	if(storage_detailForm.form.el){
            		storage_detailForm.form.el.dom.reset();            	
            	}else{
            		storage_detailForm.form.reset();
            	}
            	if(record){
            		Ext.getCmp('commitButton').setText('修改');
		    		addOrEditStorage_win.setTitle("编辑仓库");
		    		addOrEditStorage_win.isAdd = false;
		    		storage_detailForm.getForm().loadRecord(record);
		    		addOrEditStorage_win.show();
            	}else{
            		Ext.MessageBox.alert('提示','请选择要修改的明细！');
            	}    
            	
            }

            function submitForm(){
			    var from=storage_detailForm.getForm();
            	if(!from.isValid()){
	         		 return;
	      		}
	      		if(addOrEditStorage_win.isAdd){
	      		   from.submit({
	           			url:app.url+'/admin/com/createStorage.do',
	           			method :"post",
	          			success:function(response,options){
							store.reload();
							Ext.MessageBox.alert("提示","操作成功!");
							addOrEditStorage_win.hide();
	           			},
	           			failure:function(response,options){
	                		Ext.MessageBox.alert("提示","发生异常，请稍候处理!");    
	           			}
	       			});
	      		}else{
            		from.submit({
	           			url:app.url+'/admin/com/updateStorage.do',
	           			method :"post",
	          			success:function(response,options){
						    store.reload();
							Ext.MessageBox.alert("提示","修改成功!");
							addOrEditStorage_win.hide();
	           			},
	           			failure:function(response,options){
	                		Ext.MessageBox.alert("提示","发生异常，请稍候处理!");    
	           			}
	       			});
           		}
         	}
         	
            var joinPanel = [grid_storage,storageSpace_tabs];
           return joinPanel;
	},
	
    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('grid-win');
		
        if(!win){
		  
            win = desktop.createWindow({
                id: 'storage-win',
                title:'仓库管理',
                width:app.minWidth,
                height:app.minHeight,
                iconCls: 'icon-grid',
                shim:false,
                animCollapse:false,
                constrainHeader:true,
                layout: 'border',
                items : this.getPanel()
                    
            });
        }
        win.show();
    }
});